<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <base th:href="${#httpServletRequest.getContextPath()}">
    <title>添加院系</title>
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="../../static/layui/css/layui.css" th:href="@{/layui/css/layui.css}">
    <!-- 引入 layui.js -->
    <script src="../../static/layui/layui.js" th:src="@{/layui/layui.js}"></script>
</head>
<body style="width: 99%">
<div style="width: 630px">
    <div>
        <div class="layui-card">
            <div class="layui-card-body layui-bg-gray">
                <form class="layui-form" lay-filter="addUserForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">您的身份是?</label>
                        <div class="layui-input-block">
                            <input type="radio" name="identity" value="teacher" title="老师" checked="">
                            <input type="radio" name="identity" value="student" title="学生">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">id</label>
                        <div class="layui-input-block">
                            <label>
                                <input type="text" name="id" lay-verify="notNull" autocomplete="off"
                                       placeholder="请输入id" class="layui-input">
                            </label>
                        </div>
                    </div>
                    <div class="layui-btn-container" style="margin-left: 435px">
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        <button type="submit" class="layui-btn" style="margin-right: 0" lay-submit=""
                                lay-filter="addUserSubmit">绑定
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        const form = layui.form
            , layer = layui.layer
            , $ = layui.$;

        //自定义验证规则
        form.verify({
            notNull: [
                /^[\s\S]*.*[^\s][\s\S]*$/,
                '此项不能为空'
            ]
        });

        //监听提交
        form.on('submit(addUserSubmit)', function (data) {
            const identity = data.field.identity;
            $.ajax({
                url: "/user/bind/" + identity,
                type: "post",
                data: {
                    id: data.field.id
                },
                dataType: "json",
                success: function (response) {
                    let code = response.code;
                    if (code == 0) {
                        layer.msg("绑定成功!", {icon: 1, offset: "auto", time: 2000});
                        $("#reset").click();
                    } else {
                        layer.msg("绑定失败! " + response.code + " " + response.msg, {icon: 0, offset: "auto", time: 2000});
                    }
                },
                error: function (response) {
                    layer.msg("添加失败! " + response.status + " " + response.statusText, {
                        icon: 0,
                        offset: "auto",
                        time: 2000
                    });
                }
            });
            return false;
        });
    });
</script>
</html>